package com.fh.shop.api.address.biz;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.shop.api.address.mapper.IAddressMapper;
import com.fh.shop.api.address.po.Address;
import com.fh.shop.api.common.Constant;
import com.fh.shop.api.common.ResponseEnum;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.List;

@Service("addressService")
@Transactional(rollbackFor = Exception.class)
public class IAddressServiceImpl implements IAddressService {
    @Resource
    private IAddressMapper addressMapper;

    @Override
    public ServerResponse add(Address address) {
        if (StringUtils.isEmpty(address.getAddress())
                || StringUtils.isEmpty(address.getPhone())
                || StringUtils.isEmpty(address.getPhone())){
            return ServerResponse.error(ResponseEnum.ADDRESS_INFO_IS_NULL);
        }
        addressMapper.insert(address);
        //清空缓存
        String key = KeyUtil.addrKey(address.getMemberId());
        RedisUtil.del(key);
        return ServerResponse.success();
    }



    @Override
    public ServerResponse findList(Long memberId) {
        //先重缓存中取  没有在取数据库查找
        String key = KeyUtil.addrKey(memberId);
        String addrJson = RedisUtil.get(key);
        if (StringUtils.isNotEmpty(addrJson)){
            //不为空说明有数据 直接返回
            List<Address> addressList = JSON.parseArray(addrJson, Address.class);
            return ServerResponse.success(addressList);
        }
        //否则取数据库查
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("memberId",memberId);
        List<Address> addressList = addressMapper.selectList(addressQueryWrapper);
        //查到数据后往缓存中存一份 并且设置过期时间
        String addressListJson = JSON.toJSONString(addressList);
        RedisUtil.setex(key,addressListJson,Constant.CODE_EXPIRATION);
        return ServerResponse.success(addressList);
    }

    @Override
    public ServerResponse deleteAddress(Long id,Long memberId) {
        addressMapper.deleteById(id);
        //清空缓存
        String key = KeyUtil.addrKey(memberId);
        RedisUtil.del(key);
        return ServerResponse.success();
    }

    @Override
    public ServerResponse findAddressById(Long id) {
        Address address = addressMapper.selectById(id);
        return ServerResponse.success(address);
    }

    @Override
    public ServerResponse updateAddress(Address address) {
        //非空验证
        if (StringUtils.isEmpty(address.getAddress())
                || StringUtils.isEmpty(address.getPhone())
                || StringUtils.isEmpty(address.getPhone())){
            return ServerResponse.error(ResponseEnum.ADDRESS_INFO_IS_NULL);
        }
        Address entity = new Address();
        entity.setId(address.getId());
        entity.setAddress(address.getAddress());
        entity.setPhone(address.getPhone());
        entity.setReceiver(address.getReceiver());
        addressMapper.updateById(entity);
        //清空缓存
        String key = KeyUtil.addrKey(address.getMemberId());
        RedisUtil.del(key);
        return ServerResponse.success();
    }

    //修改状态
    @Override
    public ServerResponse updateStatus(Long id,Long memberId) {

        //修改会员的所有地址状态
        Address address2 = new Address();
        address2.setStatus(Constant.ADDRESS_NOT_STATUS);
        QueryWrapper<Address> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("memberId",memberId);
        addressMapper.update(address2, updateWrapper);

        //修改默认地址
        Address address = new Address();
        address.setId(id);
        address.setStatus(Constant.ADDRESS_STATUS);
        addressMapper.updateById(address);

        //清空缓存
        String key = KeyUtil.addrKey(memberId);
        RedisUtil.del(key);
        return ServerResponse.success();
    }


}
